﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Framework.Configurations.Core;
using Pixysoft.Framework.Configurations.Controller;
using System.Reflection;
using Pixysoft.Framework.Reflection;
using Pixysoft.Tools;
using Pixysoft.Framework.Configurations;
using System.Threading;
using Pixysoft;
using Pixysoft.Framework.Verifications;

namespace Pixysoft.Framework.Configurations
{
    public class XmlManager
    {
        /// <summary>
        /// 创建是个查询默认路径的查询器
        /// </summary>
        public static IXmlCommand DefaultCommand
        {
            get
            {
                string filepath = StringHelper.default_config_basepath + StringHelper.default_config_filename;

                return new XmlCommand(filepath);
            }
        }
        /// <summary>
        /// 创建一个查询指定路径的查询器
        /// </summary>
        /// <param name="filepath"></param>
        /// <returns></returns>
        public static IXmlCommand CreateCommand(string filepath)
        {
            if (string.IsNullOrEmpty(filepath))
                throw new Exception("missing filepath.");

            return new XmlCommand(filepath);
        }
        /// <summary>
        /// 创建一个内存节点
        /// </summary>
        /// <param name="elementName"></param>
        /// <returns></returns>
        public static IXmlNode CreateNode(string elementName)
        {
            if (string.IsNullOrEmpty(elementName))
                throw new Exception("missing elementname");

            return new XmlNode(elementName);
        }
        /// <summary>
        /// 构造一个xmltree
        /// </summary>
        /// <returns></returns>
        public static IXmlTree CreateTree()
        {
            return new XmlTree();
        }


        /// <summary>
        /// 解析多个根节点
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static IXmlTree DeserializeToTree(string value)
        {
            if (value == null)
                return null;

            return DeserializeToTree(Pixysoft.StringCoder.StringToByte(value));
        }
        /// <summary>
        /// 解析多个根节点
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static IXmlTree DeserializeToTree(byte[] value)
        {
            if (value == null || value.Length == 0)
                return null;

            return XmlNodeController.Instance.DeserializeToTree(value);
        }
        /// <summary>
        /// 反序列化为xml对象，仅支持一个根节点，另外的会被忽略
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static IXmlNode Deserialize(string value)
        {
            if (value == null)
                return null;

            return XmlNodeController.Instance.DeserializeToNode(value);
        }
        /// <summary>
        /// 反序列化为xml对象，仅支持一个根节点，另外的会被忽略
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static IXmlNode Deserialize(byte[] value)
        {
            if (value == null || value.Length == 0)
                return null;

            return XmlNodeController.Instance.DeserializeToNode(value);
        }
        /// <summary>
        /// 从nodefrom更新到nodeto
        /// </summary>
        /// <param name="nodeFrom"></param>
        /// <param name="nodeTo"></param>
        public static void Update(IXmlNode nodeFrom, IXmlNode nodeTo)
        {
            XmlUpdateController.Update(nodeFrom, nodeTo);
        }



        /// <summary>
        /// 对象序列化为xml节点 如果输入为空，返回null
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static IXmlNode Serialize(object value)
        {
            if (value == null)
                return null;

            return ReflectionController.Instance.Serialize(value);
        }
        /// <summary>
        /// 动态序列化 目的是兼容动态反序列化对象的标签库
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static IXmlNode DynamicSerialize(object value)
        {
            return DynamicReflectionController.Serialize(value);
        }


        /// <summary>
        /// 直接反射回对象 输入为空 则返回default(T)
        /// 不支持接口
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        public static T Deserialize<T>(string value)
        {
            return (T)Deserialize(typeof(T), value);
        }
        /// <summary>
        /// 直接反射回对象 输入为空 则返回default(T)
        /// 不支持接口
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        public static T Deserialize<T>(byte[] value)
        {
            return (T)Deserialize(typeof(T), value);
        }
        /// <summary>
        /// 直接反射回对象 输入为空 则返回default(T)
        /// 不支持接口        
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        public static object Deserialize(Type type, string value)
        {
            if (type.IsInterface)
                throw Exceptions.UnexpectedResultException("not support interface in deserialize:{0}", type.Name);

            if (string.IsNullOrEmpty(value))
                return Pixysoft.Tools.ParserHelper.GetNullValue(type);

            IXmlNode node = Deserialize(value);

            if (node == null)
                return null;

            return ReflectionController.Instance.Deserialize(type, node);
        }
        /// <summary>
        /// 直接反射回对象 输入为空 则返回default(T)
        /// 不支持接口
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        public static object Deserialize(Type type, byte[] value)
        {
            if (type.IsInterface)
                throw Exceptions.UnexpectedResultException("not support interface in deserialize:{0}", type.Name);

            if (value == null || value.Length == 0)
                return Pixysoft.Tools.ParserHelper.GetNullValue(type);

            IXmlNode node = Deserialize(value);

            if (node == null)
                return null;

            return ReflectionController.Instance.Deserialize(type, node);
        }




        /// <summary>
        /// 使用代理反序列化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        public static T PojoDeserialize<T>(string value)
        {
            if (value == null)
                return default(T);

            IXmlNode node = Deserialize(value);

            if (node == null)
                return default(T);

            return ReflectionController.Instance.PojoDeserialize<T>(node);
        }
        /// <summary>
        /// 代理反序列化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        public static T PojoDeserialize<T>(byte[] value)
        {
            if (value == null || value.Length == 0)
                return default(T);

            IXmlNode node = Deserialize(value);

            if (node == null)
                return default(T);

            return ReflectionController.Instance.PojoDeserialize<T>(node);
        }

        public static object PojoDeserialize(Type type, string value)
        {
            if (value == null)
                return Pixysoft.Tools.ParserHelper.GetNullValue(type);

            IXmlNode node = Deserialize(value);

            if (node == null)
                return Pixysoft.Tools.ParserHelper.GetNullValue(type);

            return ReflectionController.Instance.PojoDeserialize(type, node);
        }

        public static object PojoDeserialize(Type type, byte[] value)
        {
            if (value == null || value.Length == 0)
                return Pixysoft.Tools.ParserHelper.GetNullValue(type);

            IXmlNode node = Deserialize(value);

            if (node == null)
                return Pixysoft.Tools.ParserHelper.GetNullValue(type);

            return ReflectionController.Instance.PojoDeserialize(type, node);
        }



        /// <summary>
        /// 直接反射回对象 输入为空 则返回default(T)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        public static T DynamicDeserialize<T>(string value)
        {
            return (T)DynamicDeserialize(typeof(T), value);
        }
        /// <summary>
        /// 直接反射回对象 输入为空 则返回default(T)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        public static T DynamicDeserialize<T>(byte[] value)
        {
            return (T)DynamicDeserialize(typeof(T), value);
        }
        /// <summary>
        /// 动态反序列化
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static object DynamicDeserialize(Type type, string value)
        {
            IXmlNode node = Deserialize(value);

            if (node == null)
                return null;

            return DynamicReflectionController.Deserialize(type, node);
        }
        /// <summary>
        /// 动态反序列化
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static object DynamicDeserialize(Type type, byte[] value)
        {
            IXmlNode node = Deserialize(value);

            if (node == null)
                return null;

            return DynamicReflectionController.Deserialize(type, node);
        }


        /// <summary>
        /// 方法序列化 仅支持动态序列化
        /// </summary>
        /// <param name="info"></param>
        /// <param name="paras"></param>
        /// <returns></returns>
        public static IXmlNode MethodSerialize(MethodInfo info, params object[] paras)
        {
            string assemblyName = AssemblyHelper.Instance.GetAssemblyName(info.DeclaringType.Assembly);

            string typeName = info.DeclaringType.Name;

            return MethodSerialize(assemblyName, typeName, info, paras);
        }
        /// <summary>
        /// 方法序列化 仅支持动态序列化
        /// </summary>
        /// <param name="info"></param>
        /// <param name="paras"></param>
        /// <returns></returns>
        public static IXmlNode MethodSerialize(string assemblyName, string typeName, MethodInfo info, params object[] paras)
        {
            return MethodReflectionController.MethodSerialize(assemblyName, typeName, info, paras);
        }


        /// <summary>
        /// 方法结构反序列化
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static MethodStructure MethodStructureDeserialize(string value)
        {
            if (value == null)
                throw Exceptions.VerificationFailedException(value, value);

            IXmlNode node = Deserialize(value);

            return MethodReflectionController.MethodStructureDeserialize(node);
        }
        /// <summary>
        /// 方法反序列化
        /// </summary>
        /// <returns></returns>
        public static object MethodDeserialize(string value)
        {
            if (value == null)
                throw Exceptions.VerificationFailedException(value, value);

            IXmlNode node = Deserialize(value);

            return MethodReflectionController.MethodDeserialize(node);
        }
        /// <summary>
        /// 方法反序列化
        /// </summary>
        /// <returns></returns>
        public static object MethodDeserialize(string assemblyName, string typeName, string value)
        {
            if (value == null)
                throw Exceptions.VerificationFailedException(value, value);

            IXmlNode node = Deserialize(value);

            return MethodReflectionController.MethodDeserialize(assemblyName, typeName, node);
        }
        /// <summary>
        /// 状态持久的方法反序列化
        /// </summary>
        /// <returns></returns>
        public static object MethodDeserialize(object target, string value)
        {
            if (target == null)
                throw Exceptions.VerificationFailedException(target, value);

            IXmlNode node = Deserialize(value);

            return MethodReflectionController.MethodDeserialize(target, node);
        }
    }
}